package login

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/service/login_service"
	"github.com/gin-gonic/gin"
)

type LoginVerify struct {
	State string `alias:"三方参数" form:"state"  json:"state" valid:"Required;"`
	Code  string `alias:"code" form:"code"  json:"code" valid:"Required;"`
}

func Login(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form LoginVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	Service := login_service.Login{
		State: form.State,
		Code:  form.Code,
	}
	data, err := Service.Login()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type RefreshTokenVerify struct {
	AccessToken string `alias:"token" form:"access_token"  json:"access_token" valid:"Required;"`
	Refresh     string `alias:"刷新 token token" form:"refresh_token"  json:"refresh_token" valid:"Required;"`
}

func RefreshToken(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form RefreshTokenVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	var data interface{}
	Service := login_service.RefreshToken{
		AccessToken: form.AccessToken,
		Refresh:     form.Refresh,
	}
	data, err := Service.RefreshToken()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type AliRefreshTokenVerify struct {
}

func AliRefreshToken(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AliRefreshTokenVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	var data interface{}
	Service := login_service.AliRefreshToken{}
	data, err := Service.AliRefreshToken()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type DyLoginVerify struct {
	State string `alias:"三方参数" form:"state"  json:"state" valid:"Required;"`
	Code  string `alias:"code" form:"code"  json:"code" valid:"Required;"`
}

func DyLogin(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form DyLoginVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	Service := login_service.DyLogin{
		State: form.State,
		Code:  form.Code,
	}
	data, err := Service.DyLogin()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
